<template>
  <view class="allContainer">
    <!-- :circular="true"表示循环播放 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="swiper" :circular="true">
      <swiper-item v-for="(item,index) in goodInfo.pics" :key='index'>
        <view class="swiper-item">
          <image :src="item.pics_big" mode="" class="img" @click='preview(index)'></image>
        </view>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goodInfo" v-if='goodInfo.goods_name'>
      <view class="price">￥{{goodInfo.goods_price}}</view>
      <view class="main">
        <view class="name">{{goodInfo.goods_name}}</view>
        <view class="favi">
          <uni-icons type="star" size="18" color="gray" class='icon'></uni-icons>
          <text class="text">收藏</text>
        </view>
      </view>
      <view class="send">
        快递：免运费
        {{cartSUM}}
      </view>
    </view>
    <!-- 渲染html字符串 -->
    <rich-text :nodes="goodInfo.goods_introduce"></rich-text>
    <view class="nav">
      <uni-goods-nav :fill='true' :options='options' :buttonGroup='buttonGroup' @click='onClick' @buttonClick='buttonClick'></uni-goods-nav>
    </view>
  </view>
</template>

<script>
  import {reqGoodDetail} from '../../api/api.js';
  import {mapState,mapMutations,mapGetters} from 'vuex';
  export default {
    computed:{
      ...mapState('cart',['a']),
      ...mapGetters('cart',['cartSUM'])
    },
    data() {
      return {
        goodInfo:{},
        // 配置导航栏数据
        // 左侧按钮组的配置对象
        options:[{
          icon:'headphones',
          text:'客服'
        },{
          icon:'shop',
          text:'店铺'
        },{
          icon:'cart',
          text:'购物车',
          info:0
        }],
        // 右侧按钮组的配置对象
        buttonGroup:[{
          text:'加入购物车',
          backgroundColor:'#ff0000',
          color:'#fff'
        },{
          text:'立即购买',
          backgroundColor:'#ffa200',
          color:'#fff'
        }]
      }
    },
    onLoad(options) {
      const goodId=options.goods_id;
      this.getGoodDetail(goodId);
    },
    watch:{
      cartSUM:{
        immediate:true,
        handler(newValue){
          this.options[2].info=newValue;
        }
      }
    },
    methods: {
      ...mapMutations('cart',['ADDTOCART']),
      async getGoodDetail(id){
        console.log(id);
        let res=await reqGoodDetail(id);
        if(res.data.meta.status===200){
          res.data.message.goods_introduce=res.data.message.goods_introduce.replace(/<img /g,'<img style="display:block;" ').replace(/webp/g,'jpg');
          this.goodInfo=res.data.message;
        }else{
          return uni.$showMsg();
        }
      },
      preview(index){
        uni.previewImage({
          current:index,
          urls:this.goodInfo.pics.map(item=>item.pics_big)
        })
      },
      onClick(e){
        if(e.content.text==='购物车'){
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      buttonClick(e){     
        if(e.content.text=='加入购物车'){
          const goods={
            goods_id:this.goodInfo.goods_id,
            goods_name:this.goodInfo.goods_name,
            goods_price:this.goodInfo.goods_price,
            goods_count:1,
            goods_small_logo:this.goodInfo.goods_small_logo,
            goods_state:true
          };
          this.ADDTOCART(goods)
        }
      }
    }
  }
</script>


<style lang="scss" scoped>
.allContainer{
  padding-bottom: 50px;
  .swiper{
    height: 750rpx;
    .img{
      width: 100%;
      height: 750rpx;
    }
  }
  .goodInfo{
    padding: 10px;
    padding-right: 0;
    .price{
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }
    .main{
      display: flex;
      justify-content: space-around;
      .name{
        font-size: 13px;
        margin-right: 10px;
      }
      .favi{
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 1px solid #ddd;
        color: gray;
        .text{
          
        }
        .icon{
          
        }
      }
    }
    .send{
      font-size: 12px;
      color: gray;
      margin: 10px 0;
    }
  }
    .nav{
      position: fixed;
      bottom: 0;
      z-index: 999;
      left: 0;
      width: 100%;
      overflow: hidden;
    }
}
</style>
